<?php

/**
 * user actions.
 *
 * @package    samsung
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userActions extends sfActions
{

  public function executeUserScore(sfWebRequest $request) {
    $this->user = USERTable::getInstance()->find($request->getParameter('id'));
    if (!$this->user instanceof USER) {
      $this->forward404();
    }
    $scores = USER_SCORETable::getInstance()->getUserScores($request->getParameter('id'));
    $activeCourse = COURSETable::getInstance()->getCourses();
    $this->remainigCourse = count($activeCourse) - count($scores);

    $this->scores = $scores;
    $this->answerdCount = ($this->remainigCourse > 0) ? count($scores) : "All";
  }

  public function executeList(sfWebRequest $request) {
    $this->users = USERTable::getInstance()->findByIsActive(Constants::STATUS_ACTIVE);
    $this->userGroups = USER_GROUPTable::getInstance()->getUserGroupArray();
    $this->arrStatusList    = StringUtil::getStatusArray();
  }

  public function executeAdd(sfWebRequest $request) {
    $errorCollector = array();
    $this->form = new UserForm();
    $this->isEdit = false;

    if ($request->hasParameter('id')) {
      $this->isEdit = true;
      $this->form = new UserForm(true);
      $user = USERTable::getInstance()->find($request->getParameter('id'));
      $this->user = $user;
      $this->form->populateData($user);
      if (!$user instanceof USER) {
        $this->forward404();
      }
    } else {
      $this->form->setDefault(UserForm::FIELD_USER_GROUP_ID, 2);
    }
    try {
      if ($request->isMethod('post')) {
        $formData = $request->getParameter(UserForm::FORM_NAME);
        $this->form->bind($formData);

        if ($this->form->isValid()) {
          if (!$this->isEdit) {
            $user = new USER();
            $user->setUsername($formData[UserForm::FIELD_USERNAME]);
          }
          $user->setFirstName($formData[UserForm::FIELD_FIRST_NAME]);
          $user->setLastName($formData[UserForm::FIELD_LAST_NAME]);
          $user->setEmail($formData[UserForm::FIELD_EMAIL]);
          $user->setUserGroupId($formData[UserForm::FIELD_USER_GROUP_ID]);
          $user->setIsActive($formData[UserForm::FIELD_STATUS]);
          if (trim($formData[UserForm::FIELD_PASSWORD]) != "") {
            $salt = StringUtil::generateRandomString(20);
            $user->setSalt($salt);
            $user->setSalt($salt);
            $user->setPassword(StringUtil::hashPassword(trim($formData[UserForm::FIELD_PASSWORD]), $salt));
          }
          $user->save();

          $this->redirect('@user-list');
        }
      }
    } catch (Exception $e) {
      $errorCollector[] = 'Internal error!.. Please try again'.$e->getMessage();
    }
    $this->errorCollector = $errorCollector;
  }

  public function executeDelete(sfWebRequest $request) {
    $user = USERTable::getInstance()->find($request->getParameter('id'));
    if (!$user instanceof USER) {
      $this->forward404();
    }
    try {
      $user->setIsActive(Constants::STATUS_DELETED);
      $user->save();

      $this->redirect('@user-list');
    } catch (Exception $e) {

    }
  }
}
